from docx import Document
from docxcompose.composer import Composer, re
import os
import sys


def main(files, final_docx):
    new_document = Document()
    composer = Composer(new_document)
    for fn in files:
        doc = Document(fn)
        doc.add_heading(fn[fn.rindex("/") + 1 : fn.rindex(".")], 3)
        composer.append(doc)
    composer.save(final_docx)


def file_list(base_path):
    result = []
    for root, dirs, files in os.walk(base_path, topdown=False):
        for name in files:
            result.append(os.path.join(root, name))
    return result


fileList = file_list(sys.argv[1])
main(fileList, os.path.join(os.getcwd(), "result.docx"))
